﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

using Domain.Core.Specification;

using Domain.MainModule.GroupBranchCurrencies;
using Domain.MainModule.Entities;

using Infraestructure.CrossCutting.Logging;
using Infraestructure.Data.Core;
using Infraestructure.Data.Core.Extensions;
using Infraestructure.Data.MainModule.Resources;
using Infraestructure.Data.MainModule.UnitOfWork;

namespace Infraestructure.Data.MainModule.Repositories
{
    public class GroupBranchCurrencyRepository : Repository<GroupBranchCurrency>, IGroupBranchCurrencyRepository
    {
        #region constructor

        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="unitOfWork">IMainModuleUnitOfWork dependency, intented to be resolved with IoC</param>
        /// <param name="traceManager">ITraceManager context, intended to be resolved wiht IoC</param>
        public GroupBranchCurrencyRepository(IMainModuleUnitOfWork unitOfWork, ITraceManager traceManager) : base(unitOfWork, traceManager) { }

        #endregion

        #region private methods

        private GroupBranchCurrency FindGroupBranchCurrencyBySpecification(ISpecification<GroupBranchCurrency> specification)
        {
            //validate specification
            if (specification == (ISpecification<CustomerService>)null)
            {
                throw new ArgumentNullException("specification");
            }

            IMainModuleUnitOfWork context = this.UnitOfWork as IMainModuleUnitOfWork;

            if (context != null)
            {
                //perform operation in this repository
                return context.GroupBranchCurrencies.Where(specification.SatisfiedBy()).SingleOrDefault();
            }
            else
            {
                throw new InvalidOperationException(string.Format(
                                                                CultureInfo.InvariantCulture,
                                                                Messages.exception_InvalidStoreContext,
                                                                this.GetType().Name));
            }
        }

        #endregion

        #region ICustomerServiceRepository implementations

        public GroupBranchCurrency FindGroupBranchCurrency(ISpecification<GroupBranchCurrency> specification)
        {
            return this.FindGroupBranchCurrencyBySpecification(specification);
        }

        public IEnumerable<GroupBranchCurrency> FindGroupBranchCurrencies(ISpecification<GroupBranchCurrency> specification)
        {
            //validate specification
            if (specification == (ISpecification<CustomerService>)null)
            {
                throw new ArgumentNullException("specification");
            }

            IMainModuleUnitOfWork context = this.UnitOfWork as IMainModuleUnitOfWork;

            if (context != null)
            {
                //perform operation in this repository
                return context.GroupBranchCurrencies.Where(specification.SatisfiedBy());
            }
            else
            {
                throw new InvalidOperationException(string.Format(
                                                                CultureInfo.InvariantCulture,
                                                                Messages.exception_InvalidStoreContext,
                                                                this.GetType().Name));
            }
        }

        #endregion
    }
}
